//
//  HLTquzhifuTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/10/10.
//  Copyright © 2015年 hlt. All rights reserved.
//

#import "HLTquzhifuTableViewController.h"

@interface HLTquzhifuTableViewController ()

@property (weak, nonatomic) IBOutlet UITextField *jine;

@property (weak, nonatomic) IBOutlet UILabel *shopname;

@property (weak, nonatomic) IBOutlet UILabel *dengji;



@property NSInteger xuanzhe;

@end

@implementation HLTquzhifuTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    _biaozhi=0;
    
    _xuanzhe=0;
    
    self.shopname.text=self.shop_name;
    
    
    
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    NSString *str;
    if ([acc.custFlag isEqual:@"0"]) {
        str=@"您当前的会员等级是,注册用户";
        
    }else if([acc.custFlag isEqual:@"1"])
    {
        str=@"您当前的会员等级是,vip";
    }
    
    self.dengji.text=str;
    
    
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}







- (IBAction)querenzhifu:(id)sender {
    
    
    
    [EYInputPopupView popViewWithTitlenumber:@"请输入登陆密码" contentText:@"登陆密码"
                                        type:EYInputPopupView_Type_single_line_text
                                 cancelBlock:^{
                                     
                                 } confirmBlock:^(UIView *view, NSString *text) {
                                     
                                     Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                                     
                                     
                                     if ([acc.pwd isEqual:text]) {
                                         
                                         
                                         UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
                                         view1.backgroundColor=[UIColor grayColor];
                                         view1.alpha=0.4;
                                         [self.view addSubview:view1];
                                         MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
                                         indicatorView.delegate = self;
                                         indicatorView.numberOfCircles = 3;
                                         indicatorView.radius = 20;
                                         indicatorView.internalSpacing = 3;
                                         indicatorView.center = self.view.center;
                                         [indicatorView startAnimating];
                                         [self.view addSubview:indicatorView];
                                         
                                         
                                         
                                         
                                         
                                         
                                         
                                         //支付码支付接口 (user_name=支付方用户名 payToUser=收款方id，shop_id=商铺id amount=支付金额 )
                                         
                                         Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                                         
                                         //base64 编码
                                         NSString *base64pwd= [SetBase64andMd5 base64:acc.pwd ncryptOrDecrypt:kCCEncrypt];
                                         
                                         
                                         
                                         
                                         
                                         NSString *str;
                                         NSString *url;
                                         if (self.xuanzhe==0) {
                                             
                                             str=pakeyzhifu;
                                             url=[str stringByAppendingFormat:@"&user_name=%@&payToUserId=%@&shop_id=%@&amount=%@&password=%@",acc.custPhone,self.touser_id,self.shop_id,self.jine.text,base64pwd];
                                         }
                                         else
                                         {
                                             str=yuepay;
                                             url=[str stringByAppendingFormat:@"&user_name=%@&payToUser=%@&shop_id=%@&amount=%@&password=%@",acc.custPhone,self.touser_id,self.shop_id,self.jine.text,base64pwd];
                                         }
                                         
                                         
                                         [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
                                             
                                             [indicatorView stopAnimating];
                                             [view1 removeFromSuperview];
                                             
                                             NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
                                             NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
                                             
                                             if ([str isEqual:@"2"]) {
                                                 //付款成功
                                                 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"交易成功" message:ary[@"msg"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                                                 [alert show];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                             }
                                             else
                                             {
                                                 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"交易失败" message:ary[@"msg"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                                                 [alert show];
                                                 
                                             }
                                             
                                             
                                             
                                             
                                         } faliure:^(NSString *error) {
                                             
                                             [indicatorView stopAnimating];
                                             [view1 removeFromSuperview];
                                             
                                         }];

                                         
                                         
                                     }else
                                     {
                                         
                                         
                                         self.biaozhi=self.biaozhi+1;
                                         if (self.biaozhi>=5) {
                                             
                                             UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"提示" message:@"您输入的密码错误次数超过5次，将清除登陆信息，并返回上一个界面" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                                             
                                             [alert show];
                                             Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                                             //退出登录
                                             [[AccountTool sharedAccountTool] tuichu:acc];
                                             [self.navigationController popViewControllerAnimated:YES];
                                             
                                         }
                                         else
                                         {
                                             
                                             NSString *str=[NSString stringWithFormat:@"您输入的密码不对,还有%ld次输入机会",5-self.biaozhi];
                                             
                                             UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                                             
                                             [alert show];
                                         }
                                         
                                         
                                         
                                         
                                        
                                     }
                                     
                                     
                                 } dismissBlock:^{
                                     
                                 }];
    
    
    
    
    
    
    
    
    
    
}

#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        
        //将之前点击的勾去掉
        NSIndexPath *index=[NSIndexPath indexPathForRow:self.xuanzhe inSection:0];
        
        UITableViewCell *cell = [tableView  cellForRowAtIndexPath:index];
        
        cell.accessoryType=UITableViewCellAccessoryNone;//UITableViewCellAccessoryCheckmark;
        
        //将点击的cell打勾
        UITableViewCell *cell2= [tableView  cellForRowAtIndexPath:indexPath];
        
        cell2.accessoryType=UITableViewCellAccessoryCheckmark;
        self.xuanzhe=indexPath.row;
        
        
    }
    
    
    
    
    
    
    
    
}



//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screen_width, 10)];
//    headerView.backgroundColor = RGB(239, 239, 244);
//    //    headerView.backgroundColor = [UIColor redColor];
//    return headerView;
//}





@end
